﻿Imports DevExpress.XtraEditors

Public Class frmNghiemThu_CapNhat
    Dim Me_AddEdit As String
    Dim Me_Dtb As New DataTable

    Private Sub frmChietTinh_CapNhat_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Me.GridView1.Focus()
    End Sub

    Private Sub frmNghiemThu_CapNhat_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        frmNghiemThu_CapNhat_Exists = False
    End Sub

    Private Sub frmNghiemThu_CapNhat_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitGrid()
        LoadEdit()

    End Sub

    Private Sub LoadEdit()
        Dim myNtF As New NghiemThuF
        Dim Dtb As New DataTable
        Dtb = myNtF.GetMaster(Me.txtSoTc.EditValue, Me.txtSoPhieu.EditValue)
        If Dtb.Rows.Count = 1 Then
            Me.txtSoPhieu1.Text = Dtb.Rows(0).Item("sophieu").ToString.Substring(0, 2)
            Me.txtSoPhieu2.Text = Dtb.Rows(0).Item("sophieu").ToString.Substring(2, 5)
            Me.txtNgayLap.Text = Dtb.Rows(0).Item("ngaylap")
            Me.txtKhachHang.Text = Dtb.Rows(0).Item("khachhang")
            Me.txtDiaChi.Text = Dtb.Rows(0).Item("diachi")

        End If

        LoadGrid()

    End Sub

    Private Sub InitGrid()

        Dim i As Integer
        With Me.GridView1
            i = 0
            .Columns(i).FieldName = "ma"
            .Columns(i).Visible = False

            i = 1
            .Columns(i).FieldName = "ten"
            .Columns(i).Caption = "Danh mục"
            .Columns(i).Width = 200
            .Columns(i).OptionsColumn.AllowEdit = False

            i = 2
            .Columns(i).FieldName = "dvt"
            .Columns(i).Caption = "Đvt"
            .Columns(i).Width = 40
            .Columns(i).OptionsColumn.AllowEdit = False

            If Me.txtMaLoaiVatTu.EditValue = "D" Then
                i = 3
                .Columns(i).FieldName = "sldl"
                .Columns(i).Caption = "Khảo sát"
                .Columns(i).Width = 65
                .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
                .Columns(i).DisplayFormat.FormatString = "### ###.##;Null"

                i = 4
                .Columns(i).FieldName = "slntdl"
                .Columns(i).Caption = "Lắp đặt"
                .Columns(i).Width = 65
                .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
                .Columns(i).DisplayFormat.FormatString = "### ###.##;Null"

            ElseIf Me.txtMaLoaiVatTu.EditValue = "K" Then

                i = 3
                .Columns(i).FieldName = "slkh"
                .Columns(i).Caption = "Khảo sát"
                .Columns(i).Width = 65
                .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
                .Columns(i).DisplayFormat.FormatString = "### ###.##;Null"

                i = 4
                .Columns(i).FieldName = "slntkh"
                .Columns(i).Caption = "Lắp đặt"
                .Columns(i).Width = 65
                .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
                .Columns(i).DisplayFormat.FormatString = "### ###.##;Null"

            End If

        End With


    End Sub

    Private Sub LoadGrid()
        Dim myNghiemThuF As New NghiemThuF

        Me_Dtb = myNghiemThuF.GetDetail(Me.txtSoPhieu.EditValue, Me.txtMaLoaiVatTu.EditValue)
        Me.GridControl1.DataSource = Me_Dtb

        Me.GridView1.ExpandAllGroups()

    End Sub

    Private Sub cmdGhi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGhiNghiemThu.Click
        Dim myNghiemThuF As New NghiemThuF

        'cap nhat nghiem thu
        Dim s As String
        If Me.txtMaLoaiVatTu.EditValue = "D" Then
            s = "slntdl"
        ElseIf Me.txtMaLoaiVatTu.EditValue = "K" Then
            s = "slntkh"
        End If

        With Me.GridView1
            Dim i As Integer
            For i = 0 To Me.GridView1.DataRowCount - 1
                If Not myNghiemThuF.UpdateNghiemThu(Me.txtSoPhieu.EditValue, .GetRowCellValue(i, "ma"), Me.txtMaLoaiVatTu.EditValue, .GetRowCellValue(i, "slntdl")) Then
                    XtraMessageBox.Show("Ghi chi tiết vật tư lỗi tại dòng " + i.ToString, "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Exit Sub
                End If
            Next
        End With

        'cap nhat trang thai
        If Not myNghiemThuF.UpdateTrangThai(Me.txtSoTc.EditValue, Me.txtSoPhieu.EditValue, 1) Then
            XtraMessageBox.Show("Lỗi ghi trạng thái", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        XtraMessageBox.Show("Ghi dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    Private Sub btnXoaNghiemThu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaNghiemThu.Click
        Dim myNghiemThuF As New NghiemThuF

        'cap nhat trang thai
        If Not myNghiemThuF.UpdateTrangThai(Me.txtSoTc.EditValue, Me.txtSoPhieu.EditValue, 0) Then
            XtraMessageBox.Show("Lỗi ghi trạng thái", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        'cap nhat nghiem thu
        If Not myNghiemThuF.DeleteNghiemThu(Me.txtSoPhieu.EditValue, Me.txtMaLoaiVatTu.EditValue) Then
            XtraMessageBox.Show("Lỗi xóa nghiệm thu", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        XtraMessageBox.Show("Ghi dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    Private Sub AddDetail()
        'dua danh sach ma trong grid vao _dtb
        frmNghiemThu_CapNhat_Dtb = New System.Data.DataTable("MyDTB")
        frmNghiemThu_CapNhat_Dtb.Columns.Add("ma", GetType(String))
        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            frmNghiemThu_CapNhat_Dtb.Rows.Add(New Object() {Me.GridView1.GetDataRow(i).Item("ma")})
        Next

        'mo form tra cuu
        Project_Form = Me.Name
        Dim f As New frmDanhMuc_TraCuu
        f.ShowDialog()

        'them vat tu
        Dim myDanhMucF As New DanhMucF
        Dim myDtb As New DataTable
        For i = 0 To frmDanhMuc_TraCuu_Dtb.Rows.Count - 1
            myDtb = myDanhMucF.GetWhereMa(frmDanhMuc_TraCuu_Dtb.Rows(i).Item("ma"))
            If myDtb.Rows.Count = 0 Then Exit Sub

            Me_Dtb.Rows.Add()
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("ma") = myDtb.Rows(0).Item("ma")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("ten") = myDtb.Rows(0).Item("ten")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("dvt") = myDtb.Rows(0).Item("dvt")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("sldl") = 0
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("slntdl") = 0
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("slkh") = 0
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("slntkh") = 0
        Next

    End Sub

    Private Sub cmdThemVatTu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThemVatTu.Click
        AddDetail()
    End Sub

    Private Sub cmdXoaVatTu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXoaVatTu.Click
        If Me.GridView1.GetFocusedRowCellValue("sldl") = 0 And Me.GridView1.GetFocusedRowCellValue("slkh") = 0 Then
            Me.GridView1.DeleteSelectedRows()
        End If
    End Sub

End Class